package info.pinlab.pinplayer.control;

import info.pinlab.pinplayer.model.response.ResponseContent;
import info.pinlab.pinplayer.model.rule.StepRule;
import info.pinlab.pinplayer.model.task.Task;
import info.pinlab.pinplayer.model.task.TaskInstance;
import info.pinlab.pinplayer.view.TaskView;

import org.apache.log4j.Logger;

public abstract class AbstractTaskController implements TaskController{
	public static Logger logger = Logger.getLogger(AbstractTaskController.class);
	SessionController sessionController = null;
	TaskView view = null;
	Task currentTask = null;
	TaskInstance taski = null;
	EnrollController enroller;
	TimedStepController timedStepController = null;
	private boolean isNextByResp = true;
	
	int attemptN = 0;
	
	
	@Override
	public void reqNext(){
		if(view!=null){
			view.setEnabled(false);
			ResponseContent respContent = view.getResponse();
			enrollResponse(respContent);
		}
		sessionController.doNext();
//		if(sessionController!=null)
//			new Thread(new Runnable() {
//				@Override
//				public void run() {
//					sessionController.doNext();
//				}
//			}).start();		
	}

	@Override
	public void reqPrev(){
//		System.out.println("Req prev! " + sessionController);
//		view.setEnabled(false);
//		ResponseContent respContent = view.getResponse();
//		enrollResponse(respContent);
		
		sessionController.doPrev();
//		if(sessionController!=null){
//			new Thread(new Runnable() {
//				@Override
//				public void run() {
////					sessionController.doPrev();
//				}
//			}).start();		
//		}
	}

	@Override
	public void reqNextByUsr(){
		StepRule srule = currentTask.getStepRule();
		if(!srule.isNextByUsr()){
			logger.debug("Step forward not allowed for user by StepRule!");
			return;
		}
		if(!isNextByResp){ //-- if isNextByResp: it's called from enrollResponse!
			reqNext();
		}else{
			view.setEnabled(false);
			ResponseContent respContent = view.getResponse();
			enrollResponse(respContent);
		}
	}

	@Override
	public void reqPrevByUsr() {
		StepRule srule = currentTask.getStepRule();
//		System.out.println("HEllo " + srule);
		if(srule!=null && !srule.isPrevByUsr()){
			logger.debug("Step backward is not allowed for user by StepRule!");
			return;
		}
		reqPrev();
	}

	@Override
	public void setSessionController(SessionController sessionController) {
		this.sessionController = sessionController;
	}

	@Override
	public void setView(TaskView view) {
		this.view = view;
	}

	@Override
	public void setTask(TaskInstance taski){
		this.taski = taski;
		this.currentTask = taski.getTask();
		this.isNextByResp = currentTask.getStepRule().isNextByResp();
		
		int timeout = currentTask.getStepRule().getTimeout();
		if(timeout > 0){
			timedStepController = new TimedStepController(timeout, this);
		}
		
		if(view!=null)
			view.setTask(currentTask);
		if(enroller!=null){
			attemptN = enroller.getResponseN(taski);
		}
	}
	
	public void setEnrollController(EnrollController enrollController){
		enroller = enrollController;
	}
	
	public void onViewVisible(){
//		System.out.println("Visible!");
		if(timedStepController!=null){
			new Thread(timedStepController).start();
		}
	}

	@Override
	public void enrollResponse(final ResponseContent respContent){
		if(sessionController==null){
			logger.warn("No response enroller set!");
		}
//		if(currentTask.getStepRule().getMaxAttempt() >= 0   
//				&& attemptN >=  currentTask.getStepRule().getMaxAttempt()){ //-- too many attempts
//			return;
//		}
		if(currentTask.getStepRule().getMaxAttempt() >= 0){  //-- there is a constraint
			if(attemptN >=  currentTask.getStepRule().getMaxAttempt()){    //-- too many attempts
				view.setEnabled(false);
				//-- no enrolling --//
				return;
			}else{
				view.setEnabled(true);
			}
		}else{ //-- there is NO enroll constraint
			view.setEnabled(true);
		}
		attemptN++;
		if(sessionController!=null)
			new Thread(new Runnable() {
				@Override
				public void run() {
					sessionController.enrollResponse(respContent);					
					if(isNextByResp){
						sessionController.doNext();
					}
				}
			}).start();
	}
}
